package action;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import protocol.DataPackage;
import protocol.JsonPackage;
import server.QQServer;

public class ServerAction {
	public static void main(String[] args) throws IOException {
		ServerSocket server = new ServerSocket(8887);
		while(true){
			Socket sockets = server.accept();
			System.out.println("开了几次服务器");
			BufferedReader readers = new BufferedReader(new InputStreamReader(sockets.getInputStream()));
			String cmd = readers.readLine().split(" ")[1].substring(1);
			System.out.println(cmd);
			if(cmd.equals("start")){
				new QQServer().run();
			}
			else if(cmd.equals("stop")){
				System.out.println("nimei2-----------------------------------------");
				Socket socket = new Socket("127.0.0.1", 8003);
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
				BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				writer.write("jordan" + "\\" + "123" + "\n");//管理员
				writer.flush();
				
				System.out.println("send shutdown...");
				String confirm = reader.readLine();
				System.out.println(confirm);
				System.out.println("confirm...........");
				
				DataPackage pack = new DataPackage("//", "\\", DataPackage.MessageType.LOGIN,
						"test shutdown sever...", 0);
				DataPackage pack1 = new DataPackage("//", "\\", DataPackage.MessageType.LOGOUT,
						"test shutdown sever...", 0);
				writer.write(new JsonPackage(pack).getJsonString() + "\n");
				writer.flush();
				writer.write(new JsonPackage(pack1).getJsonString() + "\n");
				writer.flush();
			}
		}
	}
}
